﻿using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlobStroage
{
    public class QueryExecuter
    {
        /// <summary>
        /// Cloud storage account client
        /// </summary>
        private CloudStorageAccount accountStorage;

        /// <summary>
        /// Default Constructor - Use development storage emulator.
        /// </summary>
        public QueryExecuter()
        {
            accountStorage = CloudStorageAccount.DevelopmentStorageAccount;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="accountName">Azure storage name</param>
        /// <param name="privateKey">Azure storage private key</param>
        public QueryExecuter(string accountName, string privateKey)
        {
            accountStorage = CloudStorageAccount.Parse(String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", accountName, privateKey));
        }

        /// <summary>
        /// Retrive Performance counter data
        /// </summary>
        /// <param name="counterFullName">Counter specifier full name</param>
        /// <param name="deploymentid">Deployment id</param>
        /// <param name="roleName">Role name</param>
        /// <param name="roleInstanceName">Role instance name</param>
        /// <param name="startPeriod">Start sample date time</param>
        /// <param name="endPeriod">End sample date time</param>
        /// <returns></returns>
        public List<PerformanceData> QueryPerformanceCounter(string counterFullName, string deploymentid, string roleName,
                                                                string roleInstanceName, DateTime startPeriod, DateTime endPeriod)
        {
            PerformanceDataContext context = new PerformanceDataContext(accountStorage.TableEndpoint.ToString(), accountStorage.Credentials);
            var data = context.PerfData;

            CloudTableQuery<PerformanceData> query = null;

            query = (from d in data
                     where d.PartitionKey.CompareTo("0" + startPeriod.Ticks) >= 0
                                            && d.PartitionKey.CompareTo("0" + endPeriod.Ticks) <= 0
                                             && d.CounterName == counterFullName
                                                 && d.EventTickCount >= startPeriod.Ticks
                                                     && d.EventTickCount <= endPeriod.Ticks
                                                          && d.DeploymentId == deploymentid
                                                             && d.Role == roleName
                                                                 && d.RoleInstance == roleInstanceName
                     select d).AsTableServiceQuery<PerformanceData>();

            List<PerformanceData> selectedData = new List<PerformanceData>();
            try
            {
                selectedData = query.Execute().ToList<PerformanceData>();
            }
            catch
            {
            }
            return selectedData;
        }
    }
}
